package com.woniu.consumer;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rabbitmq.client.Channel;
import com.woniu.entity.FoodOrder;
import com.woniu.service.FoodOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Classname checkOrderStatusConsumer
 * @Description TODO
 * @Date 2025/7/22 14:26
 * @Created by pc
 */
@Component
@Slf4j
public class checkOrderStatusConsumer {
    @Resource
    private FoodOrderService foodOrderService;

    @RabbitListener(queues = {"checkQueue"})
    public void consume(String msg, Channel channel, Message message){
      log.info("check消费者收到消息：" + msg);
        try {
            // 正常消费
            // 通过订单id 去查询订单的状态
            FoodOrder foodOrder = foodOrderService.getById(msg);
            if(foodOrder.getStatus() == 1){
                foodOrder.setStatus(3);
                // QueryWrapper  组装查询条件
                // UpdateWrapper 组装 修改条件
                UpdateWrapper<FoodOrder> wrapper = new UpdateWrapper<>();
                wrapper.eq("order_id",foodOrder.getOrderId());
                foodOrderService.update(foodOrder,wrapper);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            // 消费出现异常
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
